package com.symphony.parsing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class ChangeConnSettings {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String environmentsPath = "C:\\Users\\mykhailo.tys\\workspace\\advantage\\AdvPlusRubi\\trunk\\mods\\rubicon\\environment";
		File environmentsDir = new File(environmentsPath);
		String[] subFolders = environmentsDir.list();
		
		for (int i = 0; i < subFolders.length; i++) {
			
			if (!(new File(environmentsDir + "\\" + subFolders[i]).isDirectory())) {
				continue;
			}
			
			String path = environmentsDir + "\\" + subFolders[i] + "\\res\\values\\connection_settings.xml";
			System.out.println(path);
			File from = new File(path);
			File where = new File(path);
			if (from.exists()) {
				addElementRequired(from, where, "string", "name", "connection_settings_testflighttoken", "", 0);
			}
		}
		
	}
	
	private static void addElementRequired(File from, File where, String elementName, String atribute, String atributeName, String value, int position) {
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setValidating(false);
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();

			InputStream byteStream = readFile(from.getAbsoluteFile().toString());
			InputSource is = new InputSource(byteStream);
			doc = db.parse(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Node resNode = doc.getFirstChild();
		
		//this is a place where we create element we need
		Element newElement = doc.createElement(elementName);
		newElement.setAttribute(atribute, atributeName);
		newElement.setTextContent(value);
		
//		resNode.insertBefore(newElement, resNode.getChildNodes().item(0)); //- to the begining
		resNode.appendChild(newElement); //- to the end
		
		
		writeXml(doc, where);
	}
	
	private static void writeXml(Document document, File where) {
		TransformerFactory tFactory =
				TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = tFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(document);
			FileOutputStream out = new FileOutputStream(where);
			StreamResult result = new StreamResult(out);
			transformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
	}
	
	private static InputStream readFile(String path) {
		File file = new File(path);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fis;
	}

}
